package com.company.control;

import com.company.bean.MessageBean;
import com.company.bean.UserInfoBean;
import com.company.model.UserModel;
import com.company.util.MyUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddUserServlet", value = "/AddUserServlet")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置编码值
        request.setCharacterEncoding("utf-8");

        //1.获取表单数据
        MessageBean messageBean = MyUtils.getNewInstance(MessageBean.class);
        messageBean.setName(request.getParameter("name"));
        messageBean.setSex(request.getParameter("sex"));
        messageBean.setAge(Integer.parseInt(request.getParameter("age")));
        messageBean.setIdcard(request.getParameter("idcard"));
        messageBean.setWork(request.getParameter("work"));
        messageBean.setCountry(request.getParameter("country"));
        messageBean.setTel(request.getParameter("tel"));


        //2.获取表单数据，并把表单数据保存到实例类中

        //3.使用model层进行数据保存的业务逻辑

        //4.数据保存成功之后，跳转的保存结果显示页面
        if (MyUtils.getNewInstance(UserModel.class).saveUserInfo(messageBean)){
            //跳转到添加成功页面
            request.getRequestDispatcher("WEB-INF/views/addTrue.jsp").forward(request,response);
        }else {
            //跳转到用户信息页面
            request.getRequestDispatcher("WEB-INF/user4.jsp").forward(request,response);
        }

    }


}
